package com.rjt.common.util;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;


/**  

* <p>Title: JsonUtil.java</p>  

* <p>Description: </p>  

* @author renjiangtao 

* @date 2018年7月27日  

* @version 1.0  

*/
public class JsonUtil {
	/**
	 * 将json格式化为字符串，然后根据key取值
	 * @param jsonStr
	 * @param key
	 * @return
	 */
	public static String getJsonValue(String jsonStr,String key) {
		 if(StringUtils.isEmpty(jsonStr) || StringUtils.isEmpty(key)) {
			 return "";
		 } 
		 if(key.indexOf(".")==-1) {
			 if(key.indexOf("[")!=-1) {
				 int num =Integer.parseInt(TextUtil.getSubString(key, "[", "]"));
				 key=key.substring(0, key.indexOf("["));
				return  JSON.parseObject(jsonStr).getJSONArray(key).getString(num);
			 }else {
				 return JSON.parseObject(jsonStr).getString(key);
			 }
			 
		 }else {
			 String[] keys = key.split("\\.");
			 for(int i=0;i<keys.length;i++) {
				 String tempKey=keys[i];
				 if(tempKey.indexOf("[")!=-1) {
					 int num =Integer.parseInt(TextUtil.getSubString(tempKey, "[", "]"));
					 tempKey=tempKey.substring(0, keys[i].indexOf("["));
					 jsonStr=JSON.parseObject(jsonStr).getJSONArray(tempKey).getString(num);
				 }else {
					 jsonStr=JSON.parseObject(jsonStr).getString(tempKey);
				 }
			 }
			return jsonStr;
		 }
		 
	}
	public static void main(String[] args) {
		String str="{\"extra\":{\"now\":1560473053000,\"fatal_item_ids\":[],\"logid\":\"201906140844130100230192246938A4\"},\"log_pb\":{\"impr_id\":\"201906140844130100230192246938A4\"},\"status_code\":0,\"aweme_detail\":{\"aweme_id\":\"6695349109873822979\",\"status\":{\"allow_comment\":true,\"private_status\":0,\"self_see\":false,\"is_prohibited\":false,\"is_delete\":false,\"allow_share\":true,\"is_private\":false,\"with_goods\":false,\"with_fusion_goods\":false,\"in_reviewing\":false,\"reviewed\":1,\"download_status\":0,\"aweme_id\":\"6695349109873822979\"},\"bodydance_score\":0,\"is_hash_tag\":1,\"collect_stat\":0,\"xigua_task\":{\"is_xigua_task\":false},\"is_fantasy\":false,\"is_pgcshow\":false,\"distance\":\"\",\"long_video\":null,\"text_extra\":[],\"uniqid_position\":null,\"comment_list\":null,\"geofencing\":[],\"vr_type\":0,\"item_duet\":0,\"aweme_type\":0,\"image_infos\":null,\"video\":{\"bit_rate\":[{\"play_addr\":{\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\",\"url_list\":[\"http://v5-dy.ixigua.com/0ce2ad7b10790bafd0e080fab7727ec8/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/?rc=anhpdHRvOmd1bTMzO2kzM0ApQHRAbzc7PDc1NDYzNDY3Ozc5PDNAKXUpQGczdSlAZjN2KUBmaHV5cTFmc2hoZGY7NEBsXmFeLmRxb2lfLS01LS9zcy1vI28jPjAzLzYtLy0tLjEtLi0uL2k6Yi5wIzphLXEjOmAtbyNwYmZyaF4ranQ6Iy8uXg%3D%3D\",\"http://v9-dy.ixigua.com/24ed42da937dc24bd86a8aa53b6fcbf0/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/\",\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\"],\"width\":720,\"height\":720,\"url_key\":\"v0200f880000bjlrrdt7gl1qaqdfauog_h264_540p_1257022\"},\"is_h265\":0,\"gear_name\":\"normal_540\",\"quality_type\":20,\"bit_rate\":1257022}],\"duration\":25080,\"is_h265\":0,\"height\":1024,\"play_addr_lowbr\":{\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\",\"url_list\":[\"http://v5-dy.ixigua.com/0ce2ad7b10790bafd0e080fab7727ec8/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/?rc=anhpdHRvOmd1bTMzO2kzM0ApQHRAbzc7PDc1NDYzNDY3Ozc5PDNAKXUpQGczdSlAZjN2KUBmaHV5cTFmc2hoZGY7NEBsXmFeLmRxb2lfLS01LS9zcy1vI28jPjAzLzYtLy0tLjEtLi0uL2k6Yi5wIzphLXEjOmAtbyNwYmZyaF4ranQ6Iy8uXg%3D%3D\",\"http://v9-dy.ixigua.com/24ed42da937dc24bd86a8aa53b6fcbf0/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/\",\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\"],\"width\":720,\"height\":720,\"url_key\":\"v0200f880000bjlrrdt7gl1qaqdfauog_h264_540p_1257022\"},\"has_download_suffix_logo_addr\":true,\"ratio\":\"540p\",\"download_addr\":{\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\",\"url_list\":[\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme\"],\"width\":720,\"height\":720},\"origin_cover\":{\"uri\":\"large/2641c0002489876a37baa\",\"url_list\":[\"http://p3-dy.byteimg.com/large/2641c0002489876a37baa.jpeg\",\"http://p9-dy.byteimg.com/large/2641c0002489876a37baa.jpeg\",\"http://p1-dy.byteimg.com/large/2641c0002489876a37baa.jpeg\"],\"width\":720,\"height\":720},\"caption_download_addr\":{\"url_list\":[\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme_caption\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme_caption\"],\"width\":720,\"height\":720,\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\"},\"play_addr\":{\"url_key\":\"v0200f880000bjlrrdt7gl1qaqdfauog_h264_540p_1257022\",\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\",\"url_list\":[\"http://v5-dy.ixigua.com/0ce2ad7b10790bafd0e080fab7727ec8/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/?rc=anhpdHRvOmd1bTMzO2kzM0ApQHRAbzc7PDc1NDYzNDY3Ozc5PDNAKXUpQGczdSlAZjN2KUBmaHV5cTFmc2hoZGY7NEBsXmFeLmRxb2lfLS01LS9zcy1vI28jPjAzLzYtLy0tLjEtLi0uL2k6Yi5wIzphLXEjOmAtbyNwYmZyaF4ranQ6Iy8uXg%3D%3D\",\"http://v9-dy.ixigua.com/24ed42da937dc24bd86a8aa53b6fcbf0/5d02fc06/video/m/220eee6c0265487492e947931b21c5400b5116234a6d0000aecc8c3b8ff8/\",\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&media_type=4&vr_type=0&improve_bitrate=0\"],\"width\":720,\"height\":720},\"dynamic_cover\":{\"uri\":\"262bd0007752821176a16\",\"url_list\":[\"https://p9-dy.byteimg.com/obj/262bd0007752821176a16\",\"https://p1-dy.byteimg.com/obj/262bd0007752821176a16\",\"https://p3-dy.byteimg.com/obj/262bd0007752821176a16\"],\"width\":720,\"height\":720},\"has_watermark\":true,\"download_suffix_logo_addr\":{\"uri\":\"v0200f880000bjlrrdt7gl1qaqdfauog\",\"url_list\":[\"https://aweme-hl.snssdk.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=0&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme_suffix\",\"https://api-hl.amemv.com/aweme/v1/play/?video_id=v0200f880000bjlrrdt7gl1qaqdfauog&line=1&ratio=540p&watermark=1&media_type=4&vr_type=0&improve_bitrate=0&logo_name=aweme_suffix\"],\"width\":720,\"height\":720},\"cover\":{\"uri\":\"17bb500032e9dee0a1638\",\"url_list\":[\"https://p1-dy.byteimg.com/aweme/300x400/17bb500032e9dee0a1638.jpeg\",\"https://p3-dy.byteimg.com/aweme/300x400/17bb500032e9dee0a1638.jpeg\",\"https://p9-dy.byteimg.com/aweme/300x400/17bb500032e9dee0a1638.jpeg\"],\"width\":720,\"height\":720},\"width\":576},\"user_digged\":0,\"duration\":25080,\"is_vr\":false,\"risk_infos\":{\"type\":0,\"content\":\"\",\"vote\":false,\"warn\":false,\"risk_sink\":false},\"with_promotional_music\":false,\"music\":{\"is_del_video\":false,\"sec_uid\":\"MS4wLjABAAAAefbghLc_QboN2dpnac6j2u0ONQA1K8X_TYDhKYQip0yjlvuyi5eBWrJZkP143J3i\",\"cover_hd\":{\"url_list\":[\"https://p3-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\"],\"width\":720,\"height\":720,\"uri\":\"1e17700033513a0ac4716\"},\"cover_large\":{\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\"],\"width\":720,\"height\":720},\"start_time\":0,\"position\":null,\"mid\":\"6695555437019876100\",\"is_video_self_see\":false,\"external_song_info\":null,\"cover_medium\":{\"width\":720,\"height\":720,\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\"]},\"source_platform\":23,\"status\":1,\"owner_nickname\":\"文文记事本\",\"album\":\"\",\"end_time\":0,\"offline_desc\":\"\",\"author_position\":null,\"binded_challenge_id\":0,\"unshelve_countries\":null,\"id_str\":\"6695555437019876100\",\"play_url\":{\"uri\":\"http://p9-dy.byteimg.com/obj/ies-music/1634667172352051.mp3\",\"url_list\":[\"http://p9-dy.byteimg.com/obj/ies-music/1634667172352051.mp3\"],\"width\":720,\"height\":720},\"user_count\":0,\"owner_id\":\"2044725841377331\",\"owner_handle\":\"2236706789\",\"title\":\"@文文记事本创作的原声\",\"collect_stat\":0,\"is_restricted\":false,\"author_deleted\":false,\"cover_thumb\":{\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\"],\"width\":720,\"height\":720},\"redirect\":false,\"id\":6695555437019876100,\"author\":\"文文记事本\",\"extra\":\"{\\\"has_edited\\\":0,\\\"reviewed\\\":1}\",\"schema_url\":\"\",\"duration\":26,\"is_original\":false,\"prevent_download\":false},\"rate\":12,\"share_info\":{\"share_title_other\":\"\",\"share_link_desc\":\"#在抖音，记录美好生活#你知道文文为什么这样做吗？ %s 复制此链接，打开【抖音短视频】，直接观看视频！\",\"share_signature_desc\":\"TikTok: Make Every Second Count\",\"share_desc\":\"在抖音，记录美好生活\",\"share_title_myself\":\"\",\"share_title\":\"你知道文文为什么这样做吗？\",\"bool_persist\":0,\"share_signature_url\":\"https://tiktokv.com/\",\"share_quote\":\"\",\"share_url\":\"https://www.iesdouyin.com/share/video/6695349109873822979/?region=CN&mid=6695555437019876100&u_code=-1&titleType=title\",\"share_weibo_desc\":\"#在抖音，记录美好生活#你知道文文为什么这样做吗？\"},\"promotions\":[],\"group_id\":\"6695349109873822979\",\"commerce_config_data\":null,\"desc\":\"你知道文文为什么这样做吗？\",\"nickname_position\":null,\"cmt_swt\":false,\"challenge_position\":null,\"item_comment_settings\":0,\"sort_label\":\"\",\"item_react\":0,\"interaction_stickers\":null,\"create_time\":1558953429,\"author\":{\"neiguang_shield\":0,\"bind_phone\":\"\",\"tw_expire_time\":0,\"commerce_user_level\":0,\"react_setting\":0,\"story_open\":false,\"story_count\":0,\"shield_digg_notice\":0,\"secret\":0,\"live_commerce\":false,\"download_prompt_ts\":0,\"is_star\":false,\"following_count\":0,\"need_recommend\":0,\"school_poi_id\":\"\",\"user_rate\":1,\"is_verified\":true,\"custom_verify\":\"\",\"weibo_schema\":\"\",\"uid\":\"2044725841377331\",\"has_twitter_token\":false,\"fb_expire_time\":0,\"with_shop_entry\":false,\"with_dou_entry\":false,\"comment_filter_status\":0,\"room_id\":0,\"verification_type\":1,\"create_time\":0,\"nickname\":\"文文记事本\",\"school_type\":0,\"is_discipline_member\":false,\"prevent_download\":false,\"duet_setting\":0,\"aweme_count\":0,\"live_agreement\":0,\"video_icon\":{\"uri\":\"\",\"url_list\":[],\"width\":720,\"height\":720},\"status\":1,\"sec_uid\":\"MS4wLjABAAAAefbghLc_QboN2dpnac6j2u0ONQA1K8X_TYDhKYQip0yjlvuyi5eBWrJZkP143J3i\",\"gender\":2,\"has_facebook_token\":false,\"with_commerce_entry\":false,\"youtube_channel_title\":\"\",\"short_id\":\"2236706789\",\"shield_follow_notice\":0,\"enterprise_verify_reason\":\"\",\"has_orders\":false,\"has_insights\":false,\"avatar_thumb\":{\"height\":720,\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/100x100/1e17700033513a0ac4716.jpeg\"],\"width\":720},\"hide_search\":false,\"account_region\":\"\",\"avatar_168x168\":{\"width\":720,\"height\":720,\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"http://p3-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~168x168.webp\",\"http://p9-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~168x168.webp\",\"http://p1-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~168x168.webp\"]},\"authority_status\":0,\"google_account\":\"\",\"youtube_channel_id\":\"\",\"is_gov_media_vip\":false,\"comment_setting\":0,\"relative_users\":null,\"signature\":\"\",\"weibo_verify\":\"\",\"twitter_id\":\"\",\"has_email\":false,\"new_story_cover\":null,\"followers_detail\":null,\"cover_url\":[{\"uri\":\"c8510002be9a3a61aad2\",\"url_list\":[\"https://p9-dy.byteimg.com/obj/c8510002be9a3a61aad2\",\"https://p1-dy.byteimg.com/obj/c8510002be9a3a61aad2\",\"https://p3-dy.byteimg.com/obj/c8510002be9a3a61aad2\"],\"width\":720,\"height\":720}],\"avatar_300x300\":{\"width\":720,\"height\":720,\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"http://p3-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~300x300.webp\",\"http://p9-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~300x300.webp\",\"http://p1-dy.byteimg.com/img/mosaic-legacy/1e17700033513a0ac4716~300x300.webp\"]},\"birthday\":\"1999-01-01\",\"region\":\"CN\",\"reflow_page_gid\":0,\"follower_count\":0,\"total_favorited\":0,\"is_block\":false,\"has_youtube_token\":false,\"share_info\":{\"share_qrcode_url\":{\"width\":720,\"height\":720,\"uri\":\"1e10400035947f11c00a6\",\"url_list\":[\"https://p9-dy.byteimg.com/obj/1e10400035947f11c00a6\",\"https://p1-dy.byteimg.com/obj/1e10400035947f11c00a6\",\"https://p3-dy.byteimg.com/obj/1e10400035947f11c00a6\"]},\"share_title_myself\":\"\",\"share_title_other\":\"\",\"share_url\":\"\",\"share_weibo_desc\":\"\",\"share_desc\":\"\",\"share_title\":\"\"},\"verify_info\":\"\",\"is_phone_binded\":false,\"cv_level\":\"\",\"sync_to_toutiao\":0,\"follower_status\":0,\"item_list\":null,\"live_verify\":0,\"school_name\":\"\",\"is_ad_fake\":false,\"show_image_bubble\":false,\"special_lock\":1,\"weibo_url\":\"\",\"download_setting\":-1,\"cha_list\":null,\"avatar_larger\":{\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/1080x1080/1e17700033513a0ac4716.jpeg\"],\"width\":720,\"height\":720},\"unique_id_modify_time\":1560473053,\"ad_cover_url\":null,\"language\":\"zh-Hans\",\"user_period\":0,\"shield_comment_notice\":0,\"geofencing\":[],\"ins_id\":\"\",\"apple_account\":0,\"twitter_name\":\"\",\"reflow_page_uid\":0,\"favoriting_count\":0,\"unique_id\":\"\",\"with_fusion_shop_entry\":false,\"accept_private_policy\":false,\"type_label\":null,\"location\":\"\",\"weibo_name\":\"\",\"platform_sync_info\":null,\"hide_location\":false,\"avatar_uri\":\"1e17700033513a0ac4716\",\"share_qrcode_uri\":\"1e10400035947f11c00a6\",\"is_binded_weibo\":false,\"youtube_expire_time\":0,\"user_canceled\":false,\"live_agreement_time\":0,\"constellation\":6,\"user_mode\":0,\"has_unread_story\":false,\"avatar_medium\":{\"uri\":\"1e17700033513a0ac4716\",\"url_list\":[\"https://p3-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\",\"https://p9-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\",\"https://p1-dy.byteimg.com/aweme/720x720/1e17700033513a0ac4716.jpeg\"],\"width\":720,\"height\":720},\"follow_status\":0},\"share_url\":\"https://www.iesdouyin.com/share/video/6695349109873822979/?region=CN&mid=6695555437019876100&u_code=-1&titleType=title\",\"video_labels\":null,\"is_ads\":false,\"is_relieve\":false,\"author_user_id\":2044725841377331,\"ad_link_type\":0,\"cha_list\":null,\"position\":null,\"region\":\"CN\",\"desc_language\":\"un\",\"misc_info\":\"{}\",\"is_top\":0,\"prevent_download\":false,\"origin_comment_ids\":null,\"statistics\":{\"play_count\":0,\"share_count\":314,\"forward_count\":13,\"aweme_id\":\"6695349109873822979\",\"comment_count\":1456,\"digg_count\":88494,\"download_count\":412},\"video_text\":[],\"label_top_text\":null}}";	
		
		System.out.println(getJsonValue(str,"aweme_detail.share_info.share_title"));
		System.out.println(getJsonValue(str,"aweme_detail.video.play_addr.url_list[0]"));
		System.out.println(getJsonValue(str,"aweme_detail.video.cover.url_list[0]"));
	}
	
}
